package com.java.base.jvm;

import com.java.base.unsafe.UnsafeBean;

import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;

public class UnsafeMem {
    public static void main(String[] args) {
        test();
    }


    public static void test() {
        byte[] utf8 = {
                (byte) 0b00000001,
                (byte) 0b11000010, (byte) 0b10100101,
                (byte) 0b11100110, (byte) 0b10010110, (byte) 0b10110000
        };


        long adr = UnsafeBean.get().allocateMemory(128);
        try {
            for (int i = 0; i < 128 && i < utf8.length; i++) {
                UnsafeBean.get().setMemory(adr + i, 1, utf8[i]);
//                UnsafeBean.get().putByte(adr + i, utf8[i]);
                System.out.println(utf8[i]);
            }


            byte[] bytes = new byte[6];

            for (int i = 0; i < 128 && i < bytes.length; i++) {
                bytes[i] = UnsafeBean.get().getByte(adr + i);
                System.out.println(bytes[i]);
            }
            System.out.println(new String(bytes, StandardCharsets.UTF_8));

        } finally {
            UnsafeBean.get().freeMemory(adr);
        }


        ByteBuffer byteBuffer = ByteBuffer.allocateDirect(6);
        byteBuffer.put(utf8);
        byteBuffer.put((byte)0b1);
        byte[] bytes = byteBuffer.array();


    }
}
